package com.egd.imapp.contoller.activity.login;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.contoller.activity.MainActivity;
import com.egd.imapp.model.Model;
import com.egd.imapp.model.bean.UserInfo;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

/**
 * 欢迎页面
 */
public class SplashActivity extends AppCompatActivity {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            // 如果当前的activity已经退出，那么就不在处理handler中的消息
            if (isFinishing()){
                return;
            }

            // 判断进入主页面还是的登录页面
            toMainOrLogin();

            super.handleMessage(msg);
        }
    };

    // 判断进入主页面还是的登录页面
    private void toMainOrLogin() {

        Model.getInstance().getGlobalThreadPool().execute(() -> {
            //判断当前账号是否登录过
            if (EMClient.getInstance().isLoggedInBefore()){//登录过
                //获取到当前登录用户的信息
                String hxId = EMClient.getInstance().getCurrentUser();
                UserInfo userInfo = Model.getInstance().getUserAccountDao().getAccountByHxId(hxId);
                if (userInfo == null){
                    // 获取用户信息失败，先退出用账号，再重新登录
                    Looper.prepare();
                    logout();
                    Looper.loop();
                }else {
                    // 调用登录成功后的方法
                    Model.getInstance().loginSuccess(userInfo);
                    //跳转到主页面
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(intent);
                }

            }else {// 没登录过
                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
            }

            // 结束当前页面
            finish();
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //发送2秒钟的延时消息
        handler.sendMessageDelayed(Message.obtain(), 2000);
    }

    private void logout() {
        final ProgressDialog pd = new ProgressDialog(SplashActivity.this);
        String ms = "正在退出登录...";
        pd.setMessage(ms);
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        EMClient.getInstance().logout(true,new EMCallBack() {

            @Override
            public void onSuccess() {

                // 关闭DBHelper
                Model.getInstance().getDbManager().close();

                runOnUiThread(new Runnable() {
                    public void run() {
                        pd.dismiss();
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        finish();
                    }
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pd.dismiss();
                        Toast.makeText(SplashActivity.this, "服务异常，请稍后再试...", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁消息
        handler.removeCallbacksAndMessages(null);
    }
}